/**
 * @Author: Nick Zhang <thesadboy@vip.qq.com>
 * @Description: 插件设置组件
 * @Date: 2024-06-19 17:57:37
 * @LastEditors: Nick Zhang <thesadboy@vip.qq.com>
 * @LastEditTime: 2024-07-03 13:32:00
 */
package com.nickzhang.jetbrains.file_description_plugin.configs

import com.intellij.openapi.options.ConfigurationException
import com.intellij.openapi.ui.DescriptionLabel
import com.intellij.ui.TitledSeparator
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.components.JBTextArea
import com.intellij.util.ui.FormBuilder
import com.nickzhang.jetbrains.file_description_plugin.utils.CommonUtil
import javax.swing.JPanel

class AppSettingsComponent {
    private var panel: JPanel? = null
    var state: AppSettingState = AppSettingState.getInstance()
    private val createFileAdd: JBCheckBox = JBCheckBox("创建文件时添加注释", state.createFileAdd)
    private val openFileCheck: JBCheckBox = JBCheckBox("打开文件检测并添加注释", state.openFileCheck)
    private val changeUpdate: JBCheckBox = JBCheckBox("保存自动更新", state.changeUpdate)
    private val funCommentInHeaderComment: JBCheckBox = JBCheckBox("在头部注释中生成趣味注释", state.funCommentInHeaderComment)
    private val compactComment: JBCheckBox = JBCheckBox("注释开始简短模式（/*）", state.compactComment)
    private val ignorePaths: JBTextArea = JBTextArea(state.ignorePaths)
    private val customHeaderComment: JBTextArea = JBTextArea(state.customHeaderComment)

    init {
        customHeaderComment.wrapStyleWord = true
        customHeaderComment.lineWrap = true
        ignorePaths.wrapStyleWord = true
        ignorePaths.lineWrap = true
        panel = FormBuilder.createFormBuilder()
            .addComponent(TitledSeparator("功能配置"))
            .addComponent(createFileAdd)
            .addComponent(openFileCheck)
            .addComponent(changeUpdate)
            .addComponent(funCommentInHeaderComment)
            .addComponent(compactComment)
            .addComponent(DescriptionLabel("为了兼容某些插件对注释的解析，正常不需要启用"))
            .addLabeledComponentFillVertically("忽略路径", ignorePaths)
            .addComponent(DescriptionLabel("配置需要忽略的路径，一行一条，例如 node_modules/* .idea/*"))
            .addComponent(TitledSeparator("内容配置"))
            .addLabeledComponentFillVertically("自定义头部注释", customHeaderComment)
            .addComponent(DescriptionLabel("使用JSON字符串配置，兼容koroFileHeader中的fileheader.customMade字段"))
            .addComponentFillVertically(JPanel(), 0)
            .setAlignLabelOnRight(true)
            .panel
    }

    fun getPanel(): JPanel {
        return panel!!
    }

    val isModified: Boolean
        get() {
            if (state.openFileCheck != openFileCheck.isSelected) return true
            if(state.createFileAdd != createFileAdd.isSelected) return true
            if(state.changeUpdate != changeUpdate.isSelected) return true
            if(state.funCommentInHeaderComment != funCommentInHeaderComment.isSelected) return true
            if(state.compactComment != compactComment.isSelected) return true
            if(state.customHeaderComment != customHeaderComment.text) return true
            if(state.ignorePaths != ignorePaths.text) return true
            return false
        }

    fun apply() {
        if(customHeaderComment.text != "" && CommonUtil.convertJsonStringToJson(customHeaderComment.text) == null){
            throw ConfigurationException("请检查自定义头部注释配置是否正确", "错误")
        }
        state.openFileCheck = openFileCheck.isSelected
        state.createFileAdd = createFileAdd.isSelected
        state.changeUpdate = changeUpdate.isSelected
        state.funCommentInHeaderComment = funCommentInHeaderComment.isSelected
        state.compactComment = compactComment.isSelected
        state.ignorePaths = ignorePaths.text
        state.customHeaderComment = customHeaderComment.text
    }

    fun reset() {
        openFileCheck.isSelected = state.openFileCheck
        createFileAdd.isSelected = state.createFileAdd
        changeUpdate.isSelected = state.changeUpdate
        funCommentInHeaderComment.isSelected = state.funCommentInHeaderComment
        compactComment.isSelected = state.compactComment
        ignorePaths.text = state.ignorePaths
        customHeaderComment.text = state.customHeaderComment
    }
}
